package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizArt;
import com.example.demo.po.TBizArtUser;
import com.example.demo.service.TBizArtService;
import com.example.demo.service.TBizArtUserService;
import com.example.demo.util.CodeCache;
import com.example.demo.util.UploadALiYunOOS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tBizArt")
public class TBizArtController {
    @Autowired
    TBizArtService tBizArtService;

    @Autowired
    private TBizArtUserService tBAUserService;


    @RequestMapping(value = "/list")
    public ModelAndView list(@ModelAttribute TBizArt art, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(art.getPageNum());
        page.setPageSize(art.getPageSize());
        PageResult resilt = tBizArtService.findByPage(art,page);
        List<TBizArt> list = (List<TBizArt>) resilt.getContent();

        if(list!=null&&list.size()>0){
            Map<String, String> codeMap = CodeCache.codeMap;
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            for (TBizArt art1:list){
                String sDesc =codeMap.get("salestate"+"_"+art1.getSalestate());
                art1.setSalestate(sDesc);
                art1.setStyle(codeMap.get("style"+"_"+art1.getStyle()));
                art1.setStyle1(codeMap.get("style1"+"_"+art1.getStyle1()));
                //图片转换
                if(art1.getMainpic()!=null){
                    String path = oss.styleQuery(ossClient,art1.getMainpic().toString(),"style/ys320");
                    art1.setMainpic(path);
                }

            }
            ossClient.shutdown();
            resilt.setContent(list);
        }
        view.setViewName("/art/art_list");
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }

    @RequestMapping(value = "/userList")
    public ModelAndView userList(){
        ModelAndView view = new ModelAndView();
        view.setViewName("/art/user_list");
        return  view;
    }

    @RequestMapping(value = "/getUserList")
    public String getUserList(@RequestParam(value = "page",defaultValue="1") Integer page,
                              @RequestParam(value ="limit",defaultValue="10") Integer limit,
                              @RequestParam(value ="name",defaultValue="") String name,
                              @RequestParam(value ="mobilephono",defaultValue="") String mobilephono){
        JSONObject jsonObject = new JSONObject();
        try{
            PageRequest pageRequest = new PageRequest();
            pageRequest.setPageSize(limit);
            pageRequest.setPageNum(page);
            TBizArtUser tBAUser = new TBizArtUser();
            System.out.println(name);
            if(!mobilephono.trim().equals(""))
                tBAUser.setMobilephono("%"+mobilephono+"%");
            if(!"".equals(name.trim()))
                tBAUser.setName("%"+name+"%");
            PageResult resilt = tBAUserService.findByPage(tBAUser,pageRequest);
            List<TBizArtUser> list = (List<TBizArtUser>) resilt.getContent();
            jsonObject.put("code",0);
            jsonObject.put("count",resilt.getTotalSize());
            jsonObject.put("data",list);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",1);
            jsonObject.put("msg","获取艺术家信息失败！");
        }
        return jsonObject.toJSONString();
    }


    @RequestMapping(value = "/saveInit")
    public ModelAndView saveInit(@ModelAttribute TBizArt art, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        List<Object> styleList =CodeCache.levelOneMap.get("style");
        List<Object> salestate =CodeCache.levelOneMap.get("salestate");
        if(art.getId()!=null&&!"".equals(art.getId())){
            TBizArt artinfo = tBizArtService.findById(art.getId());
            //获取图片路径
            if(artinfo.getMainpic()!=null){
                UploadALiYunOOS oss = new UploadALiYunOOS();
                OSSClient ossClient =oss.creatOSSClient();
                String path = oss.styleQuery(ossClient,artinfo.getMainpic().toString(),"style/ys320");
                artinfo.setMainpic(path);
            }
            view.addObject("art",artinfo);
//            String styleName = CodeCache.codeMap.get("style1_"+artinfo.getStyle1());
//            System.out.println("style1_"+art.getStyle1());
//            System.out.println(styleName);
//            view.addObject("style1desc",styleName);
        }
        view.addObject("style",JSON.toJSON(styleList));
        view.addObject("salestate",JSON.toJSON(salestate));
        view.setViewName("/art/art_save");
        return  view;
    }

    @RequestMapping(value = "/saveOrUpdate" ,method = RequestMethod.POST)
    public String saveOrUpdate(@RequestBody TBizArt art, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            System.out.println(art.getMainpic());
            if(tBizArtService.saveOrUpdtae(art,request)){
                
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
//            jsonObject.put("data","请求失败");
        }
        return  jsonObject.toJSONString();
    }

    @RequestMapping(value = "/delete" ,method = RequestMethod.GET)
    public String saveOrUpdate(@RequestParam(value = "id") String id, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizArtService.delete(id,request)){
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

}
